/*
 * @Description: In User Settings Edit
 * @Author: your name
 * @Date: 2019-08-21 20:32:24
 * @LastEditTime: 2019-08-22 09:40:30
 * @LastEditors: Please set LastEditors
 */
window.onload = function () {
  
  var bigstring = "";
  var takeStr = $id("takeStr");
  init();
  document.onkeydown = function(eve) {
    var e = eve || event;
    var code = e.which || e.keyCode || e.charCode;
    var ch = String.fromCharCode(code);
    if (ch === bigstring) {
      //   alert("真厉害");
      setTimeout(() => {
        init();
      }, 20);
    } else {
      setTimeout(() => {
        takeStr.className = "animated shake error";
        takeStr.style.color = "red";
      }, 20);
    }
  };
  function init() {
    takeStr.className = "";
    takeStr.style.color = "greenyellow";
    bigstring = String.fromCharCode(getRand(65, 90));
    takeStr.innerHTML = bigstring;
    takeStr.className = "animated zoomIn";
  }
  work();
  function work() {
    var mcon = $id("mcon");
    var ms = $id("ms");
    // var msel = document.querySelectorAll("option");
    var msend = $id("msend");
    var send = $id("send");
    msend.onclick = function(e) {};
    send.onclick = function() {
      var index = ms.selectedIndex; //option选中的下标
      var str = msend.value;
      var arr = str.split("");
      var mimg = "";
      var mm0 = document.createElement("div");
      mm0.className = "mm0";
      mcon.appendChild(mm0);
      for (let i = 0; i < arr.length; i++) {
        var mm = document.createElement("div");
        mm.className = "mm";
        mm0.appendChild(mm);
        mimg = document.createElement("img");
        var mspan = document.createElement("span");
        mm.appendChild(mimg);
        mm.appendChild(mspan);
        switch (index) {
          case 0:
            break;
          case 1:
            mimg.src = "img/heart.gif";
            break;
          case 2:
            mimg.src = "img/knife.gif";
            break;
          case 3:
            mimg.src = "img/shuai.gif";
            break;
          case 4:
            mimg.src = "img/jiong.gif";
            break;
        }
        mspan.innerHTML = arr[i];
      }
      // console.log(index);
      // console.log(arr);
    };
  }
  // work2();

  var save = $id("save");
  save.onclick = function() {
    var minput1 = $id("minput1");
    var minput2 = $id("minput2");
    var minput3 = $id("minput3");
    var mtable = $id("mtable");
    var mtr = document.createElement("tr");
    var mtd1 = document.createElement("td");
    mtd1.innerText = 25;
    var mtd2 = document.createElement("td");
    mtd2.innerText = minput1.value;
    var mtd3 = document.createElement("td");
    mtd3.innerText = minput2.value;
    var mtd4 = document.createElement("td");
    mtd4.innerText = dateToString(new Date());
    var mtd5 = document.createElement("td");
    mtd5.innerText = minput3.value;
    var mtd6 = document.createElement("td");
    mtd6.innerHTML = '<a href="#" class = "xq">详情</a>' + '<a href="#" class = "del">删除</a>';
    mtable.appendChild(mtr);
    mtr.appendChild(mtd1);
    mtr.appendChild(mtd2);
    mtr.appendChild(mtd3);
    mtr.appendChild(mtd4);
    mtr.appendChild(mtd5);
    mtr.appendChild(mtd6);
  };
  work2();
  function work2() { 
    var mtable2 = document.querySelector("table");
    mtable2.onclick = function (eve) { 
      var e = eve || event;
      var target = e.target || e.srcElement;
      // target.
      if (target.nodeName == "TD") {
        console.log(target.innerText);
        // console.log(target.index);
        target.innerHTML = '<input type="text" value="'+target.innerText+'">'
        console.log("td被点中了....");
        
      }
      if (target.className =="xq") {
        console.log(target.innerText);
        console.log("--");
        // eve.stopPropagation();
      }
      if (target.className =="del") {
        target.parentNode.parentNode.parentNode.removeChild(target.parentNode.parentNode);
      }
    }
  }



  work3();
  function work3() {
    var mrun = $id("rundiv");
    var mbtn = $id("mbtn");
    var mtop = $id("mtop");
    var mleft = $id("mleft");
    var arr = [];
    var brr = [];
    mrun.onmousedown = function(eve) {
      var e = eve || event;
      var x1 = e.offsetX;
      var y1 = e.offsetY;
      document.onmousemove = function(eve) {
        var e = eve || event;
        var x2 = e.clientX - x1;
        var y2 = e.clientY - y1 - 30;
        // var y2 = e.clientY - y1-(takeStr.offsetTop);

        var maxl = document.documentElement.clientWidth - 100;
        var maxt = document.documentElement.clientHeight - 100;
        x2 = x2 < 0 ? 0 : x2 > maxl ? maxl : x2;
        y2 = y2 < 0 ? 0 : y2 > maxt ? maxt : y2;
        mtop.innerText = y2;
        mleft.innerText = x2;
        arr.push(x2);
        brr.push(y2);

        mrun.style.left = x2 + "px";
        mrun.style.top = y2 + "px";
        mrun.style.cursor = "move";
        // window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty();
      };
      document.onmouseup = function() {
        this.onmousemove = null;
      };
      stopProp(e);
    };
    mbtn.onclick = function() {
      console.log("点中了aaa");
      var ar = arr.reverse();
      var br = brr.reverse();
      var i = 0;
      setInterval(() => {
        mrun.style.top = br[i] + "px";
        mrun.style.left = ar[i] + "px";
        mtop.innerText = br[i];
        mleft.innerText = ar[i];
        i++;
        // }
      }, 20);
    };
  }
};
